package com.spice.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spice.common.constant.PermissionConstant;
import com.spice.common.exception.SpiceException;
import com.spice.common.result.Response;
import com.spice.common.result.ResultCodeEnum;
import com.spice.common.util.PermissionUtil;
import com.spice.service.entity.form.EditDocumentForm;
import com.spice.service.entity.pojo.DocumentInfo;
import com.spice.service.service.DocumentInfoService;
import com.spice.service.util.ShiroUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/document")
public class DocumentController {

    @Autowired
    DocumentInfoService documentInfoService;

    @ApiOperation("获取文档，文档种类："
            + PermissionConstant.SCIENTIFIC_RESEARCH_DOC
            + ","
            + PermissionConstant.LEARNING_MATERIALS_DOC)
    @PostMapping(value = "/getAll/{type}",
            produces = "application/json;charset=UTF-8")
    public Response getAllLearningMaterialsDoc(@PathVariable("type") String type) {
        // 确认是否登录
        if (!ShiroUtil.isAuthenticated()) {
            return Response.setResult(ResultCodeEnum.NO_AUTHENTICATION);
        }
        if (PermissionUtil.getDocTypeList().contains(type)) {
            if (!ShiroUtil.checkPermission(PermissionUtil
                    .combinePermission(type,
                            PermissionConstant.VIEW_PERMISSION))) {
                return Response.setResult(ResultCodeEnum.NO_PERMISSION);
            }
        } else {
            return Response.error().message("文档种类输入错误");
        }
        List<DocumentInfo> documentInfos =
                documentInfoService.getBaseMapper()
                        .selectList(new LambdaQueryWrapper<DocumentInfo>()
                                .eq(DocumentInfo::getType, type));
        return Response.ok().data("records", documentInfos);
    }

    @ApiOperation("编辑文档(save,remove),请先使用api/utils/uploadFile上传文件," +
            "再将上传文件接口所返回的FileInfo作为参数加入到documentForm中")
    @PostMapping(value = "/{method}",
            produces = "application/json;charset=UTF-8")
    public Response editDocument(
            @PathVariable("method") String method,
            @RequestBody EditDocumentForm documentForm) {
        // 确认是否登录
        if (!ShiroUtil.isAuthenticated()) {
            return Response.setResult(ResultCodeEnum.NO_AUTHENTICATION);
        }

        if (PermissionUtil.getDocTypeList().contains(documentForm.getType())) {
            if (!ShiroUtil.checkPermission(PermissionUtil
                    .combinePermission(documentForm.getType(),
                            PermissionConstant.EDIT_PERMISSION))) {
                return Response.setResult(ResultCodeEnum.NO_PERMISSION);
            }
        } else {
            return Response.error().message("文档种类错误");
        }

        if ("save".equals(method)) {
            DocumentInfo documentInfo = new DocumentInfo()
                    .setDocumentInfo(documentForm)
                    .setFileInfo(documentForm.getFileInfo());
            if (!documentInfo.isFileExist()) {
                throw new SpiceException().message("文件不存在");
            }

            if (!documentInfo.checkType()) {
                throw new SpiceException().message("文档种类错误");
            }

            if (documentInfoService.count(new LambdaQueryWrapper<DocumentInfo>()
                    .eq(DocumentInfo::getLocalFileName,
                            documentInfo.getLocalFileName())) != 0) {
                throw new SpiceException().message("目标文档已经被绑定");
            }

            if (documentInfoService.save(documentInfo)) {
                return Response.ok()
                        .message("保存成功")
                        .data("documentInfo", documentInfo);
            } else {
                return Response.error()
                        .message("保存失败")
                        .data("documentInfo", documentInfo);
            }
        } else if ("remove".equals(method)) {
            DocumentInfo documentInfo = documentInfoService.getById(documentForm.getId());
            if (documentInfo == null){
                throw new SpiceException().message("该文档id不存在");
            }
            if (documentInfoService.removeById(documentForm.getId())) {
                return Response.ok()
                        .message("删除成功")
                        .data("documentInfo", documentInfo);
            } else {
                return Response.ok()
                        .message("删除失败")
                        .data("documentInfo", documentInfo);
            }
        }
        return Response.ok();
    }

}
